﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Metadata;
using System.Web.Http.Validation;
using WooCoo.Tools;

namespace WooCoo.DataPortal.WebHost
{
    /// <summary>
    /// 表示将从HttpContent中取得数据
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Parameter, Inherited = true, AllowMultiple = false)]
    public class ActionInfoAttribute : ParameterBindingAttribute
    {
        public override HttpParameterBinding GetBinding(HttpParameterDescriptor parameter)
        {
            if (parameter == null)
            {
                throw Error.ArgumentNull("parameter");
            }
            IEnumerable<MediaTypeFormatter> formatters = parameter.Configuration.Formatters;
            IBodyModelValidator bodyModelValidator = parameter.Configuration.Services.GetBodyModelValidator();
            return new ActionInfoParameterBinding(parameter, formatters, bodyModelValidator);
        }
        internal class ActionInfoParameterBinding : HttpParameterBinding
        {
            private IEnumerable<MediaTypeFormatter> _formatters;
            private string _errorMessage;
            public override bool WillReadBody
            {
                get
                {
                    return true;
                }
            }
            public override string ErrorMessage
            {
                get
                {
                    return this._errorMessage;
                }
            }
            public IEnumerable<MediaTypeFormatter> Formatters
            {
                get
                {
                    return this._formatters;
                }
                set
                {
                    if (value == null)
                    {
                        throw Error.ArgumentNull("formatters");
                    }
                    this._formatters = value;
                }
            }
            public IBodyModelValidator BodyModelValidator
            {
                get;
                set;
            }
            public ActionInfoParameterBinding(HttpParameterDescriptor parameterDescriptor, IEnumerable<MediaTypeFormatter> formatters, IBodyModelValidator bodyModelValidator)
                : base(parameterDescriptor)
            {
                if (parameterDescriptor.IsOptional)
                {
                    this._errorMessage = "不支持可选的Body参数";
                }
                this.Formatters = formatters;
                this.BodyModelValidator = bodyModelValidator;
            }

            public override Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken)
            {
                HttpParameterDescriptor paramFromBody = base.Descriptor;
                Type type = paramFromBody.ParameterType;
                HttpRequestMessage request = actionContext.ControllerContext.Request;
                HttpContent content = request.Content;
                IFormatterLogger formatterLogger = new ModelStateFormatterLogger(actionContext.ModelState, paramFromBody.ParameterName);

            }
        }
    }
}
